//
//  InstallationImageListView.swift
//  GreenDeal
//
//  Created by Moennig Q on 13/1/2023.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
class JobImageListView: BaseImagesListView {
    //MARK: - 数据绑定
    override func bindData(){
        //MARK: - 根据id找到当前才做的checklist model，
        JobDetailModelHelper.shared.pvd_checklists.map({ list in
            if let checkModel = list.first(where: {$0.id == JobDetailModelHelper.shared.checklistModel.id}){
                return checkModel
            }
            return PVDChecklistModel()
        }).map({ $0.images }).bind(to: self.tableView.rx.items(cellIdentifier: InstallationImageTableViewCell.reuseCellIdentifier,cellType: InstallationImageTableViewCell.self)){(row, model, cell) in
            cell.setData(model,rowNumber: row)
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 社会数据
    func setData(){
        if [ChecklistFixedId.preInstall.rawValue,ChecklistFixedId.midInstall.rawValue,ChecklistFixedId.testAndCommission.rawValue,ChecklistFixedId.cocAttachment.rawValue].contains(JobDetailModelHelper.shared.checklistModel.id) == false && JobDetailModelHelper.shared.jobDetailModel.job_status != PVDJobStatusValue.installed.rawValue{//三个selfie， coc不能做删除,或者installed状态的job也不能作删除
            //MARK: - 删除图片或pdf
            self.tableView.rx.itemDeleted.bind {[weak self]  (indexPath) in
                guard let strongSelf = self else {return}
                strongSelf.deleteClosure?(indexPath.row)
            }.disposed(by: self.disposeBag)
        }
        self.descripView.setData(JobDetailModelHelper.shared.checklistModel)
        //加载数据
        JobDetailModelHelper.shared.jobDetailModel.broadCastChecklistData()
        //如果描述不会空，或者是必填项且最小数量大于零，descripView就显示
        if JobDetailModelHelper.shared.checklistModel.description.count > 0 ||  JobDetailModelHelper.shared.checklistModel.minimum_quantity > 0 {
            self.descripView.isHidden = false
            self.tableView.drawCorner(byRoundingCorners: [],radii: 0)
            self.tableView.snp.remakeConstraints { (make) in
                make.top.equalTo(self.descripView.snp.bottom)
                make.left.right.equalToSuperview()
                make.height.greaterThanOrEqualTo(0)
                make.bottom.equalTo(self.bottomView.snp.top)
            }
        }else{//否则descripView不显示
            self.descripView.isHidden = true
            self.tableView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
            self.tableView.snp.remakeConstraints { (make) in
                make.top.equalTo(topBackView.snp.bottom).offset(-20)
                make.left.right.equalToSuperview()
                make.height.greaterThanOrEqualTo(0)
                make.bottom.equalTo(self.bottomView.snp.top)
            }
        }
    }
    //MARK: - 底部按钮出事话
    func initBottomView(_ operationList: [CellModel]){
        //(Installing or unqualified) 且当前用户是installer,且不是coc
        if JobDetailModelHelper.shared.jobDetailModel.canEditJobDetailFlag && JobDetailModelHelper.shared.checklistModel.id != ChecklistFixedId.cocAttachment.rawValue{
            self.bottomView.isHidden = false
            self.bottomView.snp.remakeConstraints { make in
                make.bottom.equalTo(self.safeAreaLayoutGuide.snp.bottom).offset(Constant.BottomMargin)
                make.left.right.equalToSuperview()
                make.height.greaterThanOrEqualTo(0)
            }
        }else{
            self.bottomView.isHidden = true
            self.bottomView.snp.remakeConstraints { make in
                make.top.equalTo(self.safeAreaLayoutGuide.snp.bottom)
                make.left.right.equalToSuperview()
            }
        }
        //1.只有一个拍照按钮
        if operationList.count == 1 {
            //横分割线
            let horizionalLineView = View(hexStr: Constant.SeparatorColorHexStr)
            bottomView.addSubview(horizionalLineView)
            horizionalLineView.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.left.right.equalToSuperview()
                make.height.equalTo(1)
            }
            //都有take photo
            let takePhotoBtn = LayoutButton(.top,at: 10)
            takePhotoBtn.setBtnInfo(R.string.localizable.capturePhoto(),hexStr: "#292D32",textFont: R.font.interMedium(size: 15),imageName: "installation_detail_checklist_imagelist_takephoto_icon")
            
            takePhotoBtn.addTarget(self, action: #selector(takePhotoBtnClick), for: UIControl.Event.touchUpInside)
            if operationList.count == 1{//只有一个拍照
                bottomView.addSubview(takePhotoBtn)
                takePhotoBtn.snp.makeConstraints { make in
                    make.top.equalTo(15)
                    make.height.equalTo(55)
                    make.width.greaterThanOrEqualTo(0)
                    make.centerX.equalToSuperview()
                    make.bottom.equalTo(-10)
                }
            }
        }else{//3.两个以上的按钮
            let addAttachmentBtn = OperationButton(R.string.localizable.addDocument())
            self.bottomView.addSubview(addAttachmentBtn)
            addAttachmentBtn.snp.makeConstraints { make in
                make.bottom.equalTo(-10)
                make.left.equalTo(16)
                make.right.equalTo(-16)
                make.height.equalTo(OperationButton.height)
                make.top.equalToSuperview()
            }
            addAttachmentBtn.addTarget(self, action: #selector(addAttachmentBtnClick), for: UIControl.Event.touchUpInside)
        }
    }
    @objc func takePhotoBtnClick(){
        self.takePhotoClosure?()
    }
    
    @objc func addAttachmentBtnClick(){
        self.addAttachmentClosure?()
    }
}
